package mutilthread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 * Description: 线程池使用示例
 * User: Li_yizYa
 * Date: 2025—04—01
 * Time: 18:07
 */
public class Demo19 {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService service = Executors.newFixedThreadPool(4);
        for (int i = 0; i < 100; i++) {
            int id = i;
            service.submit(() -> {
                Thread current = Thread.currentThread();
                System.out.println("hello Thread " + id + ", "+ current.getName());
            });
        }
        // 最好不要立即终止，可能使任务还没执行完呢，县城就被终止了
        Thread.sleep(1000);
        // 把线程池里所有线程都终止掉
        service.shutdown();
        System.out.println("程序退出 ");
    }
}
